package com.example.wwk.graduationpractice.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wwk.graduationpractice.AsyncTask.RegisterAsyncTask;
import com.example.wwk.graduationpractice.R;
import com.example.wwk.graduationpractice.Globalutils.PromptShowUtils;

import org.jivesoftware.smack.AbstractXMPPConnection;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    //back
    private ImageView Back;
    //马上登录
    private TextView loginNow;
    //button 注册
    private Button registerButton;
    //edit
    private EditText password;
    private EditText userName;
    private EditText confirmPwd;
    private AutoCompleteTextView email;
    //判断两次密码输入是否一致
    private CheckBox checkPwd;
    private CheckBox checkConfirmPwd;
    //判断email是否满足格式
    private CheckBox checkEmail;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        setTitle("");
        //toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.register_toolbar);
        setSupportActionBar(toolbar);
        //马上登录
        loginNow = (TextView) findViewById(R.id.register_login_now);
        loginNow.setOnClickListener(this);
        //back
        Back = (ImageView) findViewById(R.id.back);
        Back.setOnClickListener(this);
        // 注册按钮
        registerButton = (Button) findViewById(R.id.register_button);
        registerButton.setOnClickListener(this);
        //editText
        password = (EditText) findViewById(R.id.register_password);
        confirmPwd = (EditText) findViewById(R.id.register_confirm_password);
        userName = (EditText) findViewById(R.id.register_user_name);
        email = (AutoCompleteTextView) findViewById(R.id.register_email);
        //监听是否满足要求
        password.addTextChangedListener(new EditPwdChangedListener());
        confirmPwd.addTextChangedListener(new EditConfirmPwdChangedListener());
        email.addTextChangedListener(new EditEmailChangedListener());
        //checkBox
        checkPwd = (CheckBox) findViewById(R.id.register_password_check);
        checkConfirmPwd = (CheckBox) findViewById(R.id.register_confirm_password_check);
        checkEmail = (CheckBox) findViewById(R.id.register_email_check);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_login_now:
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.back:
                Intent intent1 = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.register_button:
                //edit的内容
                String username = userName.getText().toString();
                String pwd = password.getText().toString();
                String surePwd = confirmPwd.getText().toString();
                String Email = email.getText().toString();
                if (username.equals("") || pwd.equals("") || surePwd.equals("")) {
                    PromptShowUtils.showSnackbar(registerButton, "用户名或密码不能为空！");
                } else if (pwd.equals(surePwd)) {
                    RegisterAsyncTask.register(RegisterActivity.this, username, pwd, Email);
                } else
                    PromptShowUtils.showSnackbar(registerButton, "密码不一致！");
            default:
                break;
        }
    }

    /*
    * 判断email是都满足要求
    * @param String email
    * return boolean
    */
    private boolean isEmailValid(String email) {
        if (email.length() == 0) {
            PromptShowUtils.showToast("请输入邮箱！");
            checkEmail.setChecked(false);
            return false;
        } else if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            PromptShowUtils.showToast("邮箱格式不正确!");
            checkEmail.setChecked(false);
            return false;
        } else {
            checkEmail.setChecked(true);
            return true;
        }
    }

    /*
    * 输入密码时的监听
    */
    class EditPwdChangedListener implements TextWatcher {
        private CharSequence c;

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            c = charSequence;
            String pwd = c.toString();
            pwdChange(pwd);
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            c = charSequence;
            String pwd = c.toString();
            pwdChange(pwd);
        }

        @Override
        public void afterTextChanged(Editable editable) {
            String pwd = c.toString();
            pwdChange(pwd);
        }

        private void pwdChange(String pwd) {
            if (pwd.equals("")) {
                checkPwd.setChecked(false);
            } else {
                checkPwd.setChecked(true);
            }
        }
    }

    /*
     * 输入确认密码时的监听
     */
    class EditConfirmPwdChangedListener implements TextWatcher {
        private CharSequence c;

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            c = charSequence;
            confirmpwd(c.toString());
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            c = charSequence;
            confirmpwd(c.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {
            confirmpwd(c.toString());
        }

        private void confirmpwd(String confirm) {
            if (confirm.equals(password.getText().toString()) && !confirm.equals("")) {
                checkConfirmPwd.setChecked(true);
            } else {
                checkConfirmPwd.setChecked(false);

            }
        }
    }

    /*
      * 输入email时的监听
      */
    class EditEmailChangedListener implements TextWatcher {
        private CharSequence c;

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            c = charSequence;
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            c = charSequence;
        }

        @Override
        public void afterTextChanged(Editable editable) {
            isEmailValid(c.toString());
        }
    }
}
